﻿using AmiDraw.Lib;
using AmiDraw.Lib.Factory;
using AmiDraw.Lib.Models;
using Panuon.UI.Core;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace AmiDraw.Editor.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class MultiplotImageEditViewModel
    {

        public string PlaceholderStr { get; set; } =
            "结果文本占位符: [卡片名{index:}] [卡片描述{index:}] [等级名{index:}] [等级描述{index:}] [等级概率{index:}]\n" +
            "例如 AAAA[卡片名{index:1}]AAAA 会在制定位置显示 抽到的第2张卡的 AAAA卡片名AAAA\n" +
            "结果图片占位符: [卡片图片{index:}]\n" +
            "会在指定位置显示卡片的图片，根据绘图元素的大小绘图。\n";

        public AcPackInfo PackInfo { get; set; }

        public ObservableCollection<MultiplotElemtentBase> Elements { get; set; } = new();

        public MultiplotElemtentBase SelectedElement { get; set; }


        public ICommand AddRectElementCommand { get; set; }
        public ICommand AddImageElementCommand { get; set; }
        public ICommand AddTextElementCommand { get; set; }

        public ICommand SaveCommand { get; set; }

        public ICommand LayerUpCommand { get; set; }
        public ICommand LayerDownCommand { get; set; }
        public ICommand DeleteElementCommand { get; set; }

        public ICommand AddImageCommand { get; set; }

        public ICommand SetPlaceholdStrCommand { get; set; }
        public ICommand SetPlaceholdStrCommand0 { get; set; }
        public ICommand SetPlaceholdStrCommand1 { get; set; }
        public ICommand SetPlaceholdStrCommand2 { get; set; }
        public ICommand SetPlaceholdStrCommand3 { get; set; }
        public ICommand SetPlaceholdStrCommand4 { get; set; }
        public ICommand SetPlaceholdStrCommand5 { get; set; }
        public ICommand SetPlaceholdStrCommand6 { get; set; }
        public ICommand SetPlaceholdStrCommand7 { get; set; }
        public ICommand SetPlaceholdStrCommand8 { get; set; }
        public ICommand SetPlaceholdStrCommand9 { get; set; }




        public MultiplotImageEditViewModel()
        {
            AddRectElementCommand = new RelayCommand(AddRectElement);
            AddImageElementCommand = new RelayCommand(AddImageElement);
            AddTextElementCommand = new RelayCommand(AddTextElement);
            SaveCommand = new RelayCommand(Save);
            LayerUpCommand = new RelayCommand(LayerUp);
            LayerDownCommand = new RelayCommand(LayerDown);
            DeleteElementCommand = new RelayCommand(DeleteElement);
            AddImageCommand = new RelayCommand(AddImage);
            SetPlaceholdStrCommand = new RelayCommand(SetPlaceholdStr);

            SetPlaceholdStrCommand0 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,0));
            SetPlaceholdStrCommand1 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,1));
            SetPlaceholdStrCommand2 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,2));
            SetPlaceholdStrCommand3 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,3));
            SetPlaceholdStrCommand4 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,4));
            SetPlaceholdStrCommand5 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,5));
            SetPlaceholdStrCommand6 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,6));
            SetPlaceholdStrCommand7 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,7));
            SetPlaceholdStrCommand8 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,8));
            SetPlaceholdStrCommand9 = new RelayCommand(obj=> SetPlaceholdStr_Num(obj,9));





        }

        public void Load()
        {
            var dir = PackEditViewModel.instance.DirectoryInfo.FullName;
            var multiplotdrawer = Path.Combine(dir, "multiplotdrawer.json");
            Elements = new(JsonFactory.LoadMultiplotElementJson(multiplotdrawer));

        }

        private void AddTextElement(object obj)
        {
            Elements.Add(new TextElement() { Width = 100, Height = 100 });
        }
        private void AddRectElement(object obj)
        {
            Elements.Add(new RectangleElement() { Width = 100, Height = 100 });
        }
        private void AddImageElement(object obj)
        {
            Elements.Add(new ImageElement() { Width = 100, Height = 100 });
        }
        private void Save(object obj)
        {
            var dir = PackEditViewModel.instance.DirectoryInfo.FullName;
            var multiplotdrawer = Path.Combine(dir, "multiplotdrawer.json");
            JsonFactory.WriteJson(multiplotdrawer, Elements.Select(x => x as object));
            MessageBox.Show("保存成功!奥里给!");
        }

        private void LayerUp(object obj)
        {
            var _obj = obj as MultiplotElemtentBase;
            var index = Elements.IndexOf(_obj);
            MessageBox.Show(index.ToString());
            if (index >= Elements.Count - 1)
            {
                return;
            }
            Elements.Insert(index + 2, (MultiplotElemtentBase)_obj.Clone());
            Elements.RemoveAt(index);

        }

        private void LayerDown(object obj)
        {
            var _obj = obj as MultiplotElemtentBase;

            var index = Elements.IndexOf(_obj);
            if (index <= 0)
            {
                return;
            }
            Elements.Insert(index - 1, (MultiplotElemtentBase)_obj.Clone());
            Elements.RemoveAt(index + 1);
        }

        private void DeleteElement(object obj)
        {
            Elements.Remove((MultiplotElemtentBase)obj);
        }

        private void AddImage(object obj)
        {
            if (CommonOperateHelper.ImportImage(null, out var file))
            {
                (obj as ImageElement).ImageResKey = Path.GetFileName(file);

            }
        }
        private void SetPlaceholdStr(object obj)
        {
            (obj as ImageElement).ImageResKey = @"[卡片图片{index:<序号(尖括号也要换掉)>}]";

        }

        private void SetPlaceholdStr_Num(object obj, int number)
        {

            (obj as ImageElement).ImageResKey = @"[卡片图片{index:" + number + "}]";
        }
    }
}
